package com.he.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.he.pojo.ServiceResult;
import com.he.pojo.User;
import com.he.services.impl.UserServiceImpl;
import com.he.util.JwtUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @description: 登录的Controller
 * @author: xin.he
 * @create: 2021/8/8 20:36
 **/
@RestController
@RequestMapping("/hello")
public class HelloController {

    private static final Logger log = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    UserServiceImpl userService;


    @PostMapping("/login")
    public ServiceResult login(@RequestParam(value = "username") String username,
                               @RequestParam(value = "password") String password) {
        User user = userService.queryByName(username);
        //登录失败
        if (user == null || !user.getPwd().equals(password)) {
            log.error("登录失败,账号或密码错误!");
            return ServiceResult.failure("91", "登录失败,账号或密码错误!");
        } else {
            String token = JwtUtils.createToken(user.getUid(), user.getName());
            return ServiceResult.success(token);
        }
    }

    @GetMapping("/info")
    public ServiceResult info(@RequestParam(value = "token") String token) {
        log.info("info请求,token:{}", token);
        try {
            Claims claims = Jwts.parser()
                    .setSigningKey("hello")
                    .parseClaimsJws(token)
                    .getBody();
            Integer id = Integer.valueOf(claims.getId());
            log.info("info请求,token中的username:{}", claims.getSubject());
            User user = userService.queryByUid(id.toString());
            if (user != null) {
                return ServiceResult.success(user);
            } else {
                return ServiceResult.failure("92", "token错误或已失效");
            }
        } catch (Exception e) {
            return ServiceResult.failure("92", "token错误或已失效");
        }
    }

    @GetMapping("/dashboard")
    public ServiceResult getAuthMenu(@RequestParam(value = "token") String token) {

        String menuList = "[\n" +
                "        {\n" +
                "            \"create_time\": \"2018-03-16 11:33:00\",\n" +
                "            \"menu_type\": \"M\",\n" +
                "            \"children\": [\n" +
                "                {\n" +
                "                    \"create_time\": \"2018-03-16 11:33:00\",\n" +
                "                    \"menu_type\": \"C\",\n" +
                "                    \"children\": [],\n" +
                "                    \"parent_id\": 1,\n" +
                "                    \"menu_name\": \"用户管理\",\n" +
                "                    \"icon\": \"#\",\n" +
                "                    \"perms\": \"system:user:index\",\n" +
                "                    \"order_num\": 1,\n" +
                "                    \"menu_id\": 4,\n" +
                "                    \"url\": \"/system/user\"\n" +
                "                },\n" +
                "                {\n" +
                "                    \"create_time\": \"2018-12-28 10:36:20\",\n" +
                "                    \"menu_type\": \"M\",\n" +
                "                    \"children\": [\n" +
                "                        {\n" +
                "                            \"create_time\": \"2018-12-28 10:50:28\",\n" +
                "                            \"menu_type\": \"C\",\n" +
                "                            \"parent_id\": 73,\n" +
                "                            \"menu_name\": \"人员通讯录\",\n" +
                "                            \"icon\": null,\n" +
                "                            \"perms\": \"system:person:index\",\n" +
                "                            \"order_num\": 1,\n" +
                "                            \"menu_id\": 74,\n" +
                "                            \"url\": \"/system/book/person\"\n" +
                "                        }\n" +
                "                    ],\n" +
                "                    \"parent_id\": 1,\n" +
                "                    \"menu_name\": \"通讯录管理\",\n" +
                "                    \"icon\": \"fa fa-address-book-o\",\n" +
                "                    \"perms\": null,\n" +
                "                    \"order_num\": 1,\n" +
                "                    \"menu_id\": 73,\n" +
                "                    \"url\": \"#\"\n" +
                "                }\n" +
                "            ],\n" +
                "            \"parent_id\": 0,\n" +
                "            \"menu_name\": \"系统管理\",\n" +
                "            \"icon\": \"fa fa-adjust\",\n" +
                "            \"perms\": null,\n" +
                "            \"order_num\": 2,\n" +
                "            \"menu_id\": 1,\n" +
                "            \"url\": \"#\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"create_time\": \"2018-03-16 11:33:00\",\n" +
                "            \"menu_type\": \"M\",\n" +
                "            \"children\": [\n" +
                "                {\n" +
                "                    \"create_time\": \"2018-03-16 11:33:00\",\n" +
                "                    \"menu_type\": \"C\",\n" +
                "                    \"parent_id\": 2,\n" +
                "                    \"menu_name\": \"数据监控\",\n" +
                "                    \"icon\": \"#\",\n" +
                "                    \"perms\": \"monitor:data:view\",\n" +
                "                    \"order_num\": 3,\n" +
                "                    \"menu_id\": 15,\n" +
                "                    \"url\": \"/system/druid/monitor\"\n" +
                "                }\n" +
                "            ],\n" +
                "            \"parent_id\": 0,\n" +
                "            \"menu_name\": \"系统监控\",\n" +
                "            \"icon\": \"fa fa-video-camera\",\n" +
                "            \"perms\": null,\n" +
                "            \"order_num\": 5,\n" +
                "            \"menu_id\": 2,\n" +
                "            \"url\": \"#\"\n" +
                "        }\n" +
                "    ]";
        JSONArray jsonArray = JSONArray.parseArray(menuList);
        JSONObject data = new JSONObject();
        data.put("menuList",jsonArray);

        log.info("dashboard请求,token:{}", token);
        try {
            Claims claims = Jwts.parser()
                    .setSigningKey("hello")
                    .parseClaimsJws(token)
                    .getBody();
            Integer id = Integer.valueOf(claims.getId());
            log.info("info请求,token中的username:{}", claims.getSubject());
            User user = userService.queryByUid(id.toString());
            if (user != null) {
                data.put("user" ,user);
                return ServiceResult.success(data);
            } else {
                return ServiceResult.failure("92", "token错误或已失效");
            }
        } catch (Exception e) {
            return ServiceResult.failure("92", "token错误或已失效");
        }
    }

}
